﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.IService;
using CompanyManageSystem.Model;
using CompanyManageSystem.Model.View;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompanyManageSystem.Service
{
    public class BookService :IBookService
    {
        PSContext _db;
        public BookService()
        {
            _db = new PSContext();
        }

        /// <summary>
        /// 获取所有物料分类
        /// </summary>
        /// <returns></returns>
        public List<BaseElement> GetAllMaterialCategory()
        {
            return _db.MaterialCategorys.ToList();
        }

        /// <summary>
        /// 根据物料分类ID获取供应商
        /// </summary>
        /// <returns></returns>
        public List<Supplier> GetAllSupplierByCategoryID(Guid categoryId)
        {
            //从物料分类到物料，再选择物料里面的非重复供应商ID
            var supplierId = _db.Materials.Where(p => p.CategoryID == categoryId).Select(p => p.SupplierID).Distinct();

            //供应商
            var supplier = _db.Suppliers.Where(p => supplierId.Contains(p.ID)).OrderBy(p => p.EnterTime).ToList();

            return supplier;
        }

        /// <summary>
        /// 根据物料分类ID和供应商ID获取视图明细
        /// </summary>
        /// <returns></returns>
        public List<MaterialCategorySupplierView> GetDetailBySupplierID(Guid categoryId, Guid supplierId)
        {
            //根据物料分类获取物料ID
            var materialId = _db.Materials.Where(p => p.CategoryID == categoryId).Select(p => p.ID).Distinct();
            var list = _db.MaterialCategorySupplierViews.Where(p => materialId.Contains(p.MaterialID) && p.SupplierID == supplierId)
                          .Distinct().OrderBy(p => p.CreateTime).ToList();
            return list;
        }

        /// <summary>
        /// 根据日期，分类ID和供应商ID获取视图明细
        /// </summary>
        /// <returns></returns>
        public List<MaterialCategorySupplierView> GetDetailByFactor(DateTime date1, DateTime date2, Guid categoryId, Guid supplierId)
        {
            //获取物料ID
            var materialId = _db.Materials
                                .Where(p => p.CategoryID == categoryId)
                                .Select(p => p.ID)
                                .ToList();

            //获取视图明细
            var detail = _db.MaterialCategorySupplierViews
                            .Join(_db.Materials, p => p.MaterialID, p => p.ID, (a, b) => new { a, b })
                            .Where(p => p.a.SupplierID == supplierId && p.a.MaterialID == p.b.ID)
                            .WhereIf(date1 != DateTime.MinValue, p => DbFunctions.TruncateTime(p.a.CreateTime) >= date1)
                            .WhereIf(date2 != DateTime.MinValue, p => DbFunctions.TruncateTime(p.a.CreateTime) <= date2)
                            .Select(p => p.a)
                            .OrderBy(p => p.CreateTime)
                            .ToList();
            return detail;
        }

    }
}
